package com.customview.customPath;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.customview.R;

public class CustomPath2 extends View {

    Bitmap bitmap;
    Paint mPaint;
    Paint LinePaint;
    Paint mRaindropPaint;

    public CustomPath2(Context context) {
        this(context, null);
    }

    public CustomPath2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomPath2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {

        mPaint = new Paint();
        LinePaint = new Paint();
        mRaindropPaint = new Paint();
        mRaindropPaint.setAntiAlias(true);
        mRaindropPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStyle(Paint.Style.FILL);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);

//        test1();


//        test2();

//        test3();
//        test4();


    }

    int roate = 0;

    private void test6() {

        /**
         * SweepGradient
         *
         * 它的意思是梯度渐变，也称之为扫描式渐变，因为其效果有点类似雷达的扫描效果，他也有两个构造方法：
         */
        mPaint.setShader(new SweepGradient(getWidth() / 2, getHeight() / 2, Color.RED, Color.BLUE));


    }


    private void test3() {
        //顾名思义，这个应该是重复模式，和镜像十分类似，但是不是翻转复制，而是平移复制。我们接着把上面镜像的代码的X轴模式进行修改：
        mPaint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT));

    }

    private void test2() {
        //MIRROR  镜像效果  沿着边界 copy 复制
        mPaint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.MIRROR, BitmapShader.TileMode.MIRROR));
    }

    private void test1() {
        //CLAMP作用是 拉升边界的一个像素  填充
        mPaint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

    }

    // TODO: 渐变  2019/9/25 注： getwidth getheght 在构造方法 是还没有值得 需要在onmeasure方法后才有值得 在ondraw就可以用了
    private void test4() {
        //public LinearGradient (float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

        //这是LinearGradient最简单的一个构造方法，参数虽多其实很好理解（x0，y0）表示渐变的起点坐标而（x1，y1）
        // 则表示渐变的终点坐标，这两点都是相对于屏幕坐标系而言的，而color0和color1则表示起点的颜色和终点的颜色。TileMode和上面讲的完全一致
        mPaint.setShader(new LinearGradient(0, 0, getWidth(), getHeight(), Color.RED, Color.TRANSPARENT, Shader.TileMode.CLAMP));
    }

    // TODO: 渐变   也可以设置多颜色渐变  和颜色的比例
    private void test5() {
        /**
         * todo  new float 是个梯度范围  ， 跟 new int[] 对应
         * 0f-0.4f区域 是 红 渐变起点 到 绿 渐变起点的  比例，    以此类推 0.4f-0.8f 绿的--蓝色
         */
        mPaint.setShader(new LinearGradient(0, 0, getWidth(), getHeight(), new int[]{Color.RED, Color.GREEN, Color.BLUE}, new float[]{0F, 0.4F, 0.8F}, Shader.TileMode.REPEAT));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        test4();
//        test6();


//        test9(canvas);

//        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
//        canvas.drawCircle(400, 400, 150, mPaint);

//        test9();

        test10(canvas);
    }

    // TODO: 2019/10/11  RadialGradient

    /**
     * RadialGradient (float centerX, float centerY, float radius, int centerColor, int edgeColor, Shader.TileMode tileMode)
     * <p>
     * （centerX，centerY）是圆心的坐标，radius是半径，centerColor是边缘的颜色，edgeColor是外围的颜色，最后是模式。
     *
     * @param canvas
     */
    private void test10(Canvas canvas) {

        float radios=0;
        if (getWidth()<=getHeight()) {
            radios=getWidth()/2;
            Log.i("test", "test10 width: "+radios);
        }else {
            radios=getHeight()/2;
            Log.i("test", "test10: height"+radios);

        }
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(80);
        mPaint.setAntiAlias(true);
//        mPaint.setColor(Color.BLUE);
//        mPaint.setShader(new RadialGradient(getWidth() / 2, getHeight() / 2,
//                radios, Color.WHITE, ColorBitmapShader.RED, Shader.TileMode.REPEAT));
        mPaint.setShader(new RadialGradient(getWidth() / 2, getHeight() / 2,
                radios,new int[]{Color.WHITE,Color.RED,Color.WHITE},new float[]{0.7f,0.8f,0.95f}, Shader.TileMode.CLAMP));
        canvas.drawCircle(getWidth() / 2, getHeight() / 2,
                radios, mPaint);


    }


    float currentDegress = 0;
    int maxDegress = 720;

    float s = 3000;

    boolean flag = false;

    Object object = new Object();

    private void test9() {


/*        if (flag) {
            return;

        } else {
            flag = true;

            final ValueAnimator con = ValueAnimator.ofInt(0, maxDegress);
            con.setDuration(s);
            con.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int animatedValue = (int) animation.getAnimatedValue();
                    currentDegress = animatedValue;
                    Log.d("test", animatedValue + "");
                    if (currentDegress < maxDegress) {
                        postInvalidate();
                    } else {
                        postInvalidate();

                    }
                }
            });
            con.setRepeatCount(ValueAnimator.INFINITE);
            con.start();


        }*/

        currentDegress = (currentDegress + (360 / s / 60 * 1000)) % 360;
        postInvalidate();

    }


    public void test7(Canvas canvas) {

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);

        canvas.translate(getWidth() / 2, getHeight() / 2);
        canvas.rotate(currentDegress, 50, 50);
        canvas.drawBitmap(bitmap, 0, 0, mPaint);

    }

    int GroupCount = 5;

    public void test9(Canvas canvas) {

        canvas.translate(getWidth() / 2, getHeight() / 2);

        //画多个雷达圈

        if (true) {
            mRaindropPaint.setColor(Color.BLACK);
            for (int i = 0; i < GroupCount; i++) {
                int radios = getWidth() / 2 - getWidth() / 2 / GroupCount * i;
                canvas.drawCircle(0, 0, radios, mRaindropPaint);
            }
        }


        //画十字架
        LinePaint.setColor(Color.RED);
        canvas.drawLine(-getWidth() / 2, 0, getWidth() / 2, 0, LinePaint);
        canvas.drawLine(0, -getWidth() / 2, 0, getWidth() / 2, LinePaint);


        //画扫描点
        scandiot(canvas);

        //画扫描
        mPaint.setShader(new SweepGradient(0, 0, new int[]{Color.TRANSPARENT, Color.RED}, new float[]{0.6F, 1F}));

        canvas.rotate(currentDegress, 0, 0);
        canvas.drawCircle(0, 0, getWidth() / 2, mPaint);

    }

    private void scandiot(Canvas canvas) {


    }
}
